define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 'shiftform', 'modalSearch'],
function($, validate) {
  return {
    
    init: function(data) {
      this.initPage();
      this.initEmployee();
      this.initDispatchAdjust();
      this.changeDispatchAdjust();
      this.submit(data);
    },
    
    initPage: function() {
      
      $(document).find('form').bt_validate();
      
      $('.btn-remove').on('click', function(e){
          $('input').val('');
      });
      
      $('select[name="distanceAdjustType"]').select2({
          theme: "classic",
          placeholder : '请选择',
          minimumResultsForSearch : -1,
          allowClear : true,
      });
      
      $('select[name="oilTypeId"]').select2({
          theme: "classic",
          placeholder : '请选择',
          minimumResultsForSearch : -1,
          allowClear : true,
      });
        
      
      $('input[name="otherFees"]').on('change', function() {
        
        $("input[name='otherFees']").each(function(){
          
          var inputValue = $(this).val().trim();
          
          if(parseFloat(inputValue) != 0) {
            $(this).next().show();
          } else {
            $(this).next().hide();
          }
          
        })
        
      });
        
    },
    
    initEmployee:function() {
        
        var choseEmployees = {
           url: function(){
                var url = window.__CTX__.root + '/employees/query';
                return url + "?companyIds=" + $("input[name='organizationId']").val();
             },
           tableColumns: ['员工代码', '员工名称'],
           tableValues: ['code', 'name'],
           displayValues: ['code', 'name'],
           values: ['id']
        }
        
        $('[data-target="#modal-search-eId"]').modalsearch(choseEmployees);
        $('[data-target="#modal-search-fId"]').modalsearch(choseEmployees);
        $('[data-target="#modal-search-gId"]').modalsearch(choseEmployees);
        $('[data-target="#modal-search-hId"]').modalsearch(choseEmployees);
        
        var options = {
          time: 1000,
          url: window.__CTX__.root + "/employees/query",
          queryName: "code",
          value: ["id"],
          display: ["code", "name"]
        }
      
        $('.eId-search').koalaSearch(options);
        $('.fId-search').koalaSearch(options);
        $('.gId-search').koalaSearch(options);
        $('.hId-search').koalaSearch(options);
    },
    
    initDispatchAdjust : function() {
    	
    	$(document).ready(function(e){
    		
    		var adjustType = $("select[name='distanceAdjustType']").val();
    		if(adjustType === "无需调整"){
    			$("input[name='adjustmentDistance']").attr("readonly", true); 
    			$("textarea[name='distanceAdjustRemark']").attr("readonly", true); 
    		}
    		
    	})
    },
    
    changeDispatchAdjust : function(){
    	
    	$("select[name='distanceAdjustType']").change(function(){
    		
    		var adjustType = $("select[name='distanceAdjustType']").val();
    		
    		if(adjustType !== "无需调整"){
    			$("input[name='adjustmentDistance']").attr("readonly", false); 
    			$("textarea[name='distanceAdjustRemark']").attr("readonly", false); 
    		} else {
    			$("input[name='adjustmentDistance']").val('0.00');
    			$("input[name='adjustmentDistance']").attr("readonly", true);
    			$("textarea[name='distanceAdjustRemark']").val("");
    			$("textarea[name='distanceAdjustRemark']").attr("readonly", true);
    		}
    		
    	});
    	
    },
    
    submit : function(data) {
        
        $("#validate-form").shiftform({
            beforeSubmit: function (el) {
            	
            	var validateResult = true;
            	
            	//1. validate driver, main_driver cannot be same as first_vice_driver
              var mainDriver = $('input[name="mainDriver"]').val().trim();
              var firstViceDriver = $('input[name="firstViceDriver"]').val().trim();
              
              if(mainDriver != null && mainDriver!= undefined && mainDriver!="" && mainDriver === firstViceDriver){
                  $("input[name='firstViceDriver']").show_err_tooltip('请不要重复选择驾驶员！');
                  validateResult = false;
              }
              
              //2. validate full_load_number
              var actualLoadNumber = data.actualLoadNumber;
              var inputLoadNumber = $('input[name="fullLoadNumber"]').val().trim();
              if(parseInt(actualLoadNumber) > parseInt(inputLoadNumber)){
                  $('input[name="fullLoadNumber"]').show_err_tooltip('实际装载数量不能大于装载能力！');
                  validateResult = false;
              }
              
              //3. validate the adjustmentDistance and distanceAdjustRemark when distanceAdjustType not "无需调整"
              var adjustType = $("select[name='distanceAdjustType']").val();
              if(adjustType !== "无需调整") {
              	
              	var adjustmentDistance = $("input[name='adjustmentDistance']").val(); 
              	var distanceAdjustRemark = $("textarea[name='distanceAdjustRemark']").val();
              	
              	if(adjustmentDistance === "0.00" || adjustmentDistance === ''){
                  $('input[name="adjustmentDistance"]').show_err_tooltip('调整数不能为0！');
                  validateResult = false;
              	}
              	
              	if(adjustType === "其它" && !distanceAdjustRemark){
                  $('input[name="distance-adjust-remark"]').show_err_tooltip('请填写行驶公里数调整理由！');
                  validateResult = false;
              	}
              }
              
              //4.validate other fee auditor
              var isSet = false;
              
              $("input[name='otherFees']").each(function(){
              	
              	var inputValue = $(this).val().trim();
              	
              	if(parseFloat(inputValue) != 0) {
              	  isSet = true;
              	  return false;
              	}
              	
              })
              
              // 5. validate 行驶公里数调整为0时，调整理由不可填写
              var adjustmentDistance = $("input[name='adjustmentDistance']").val(); 
              var distanceAdjustRemark = $("textarea[name='distanceAdjustRemark']").val();
              if (adjustmentDistance == 0 && distanceAdjustRemark !== '') {
                $('input[name="adjustmentDistance"]').show_err_tooltip('行驶公里数调整为0时，调整理由不可填写！');
                validateResult = false;
              }
              
              // 6. validate otherFeeAuditor
              var otherFeeAuditor = $("input[name='otherFeeAuditor']").val();
              if(isSet && !otherFeeAuditor){
                $('input[name="otherFeeAuditor"]').show_err_tooltip('请填写其他费用审核人！');
                validateResult = false;
              }
                
            if(validateResult === false) {
              return false;
            }
            
            var roadFee = $('input[name="roadFee"]').val();
            var roadFeeConfirm = true;
            if (roadFee == 0) {
              roadFeeConfirm = confirm("路桥费等于0, 确认继续吗？");
            }
            
            //5. submit
            $("#validate-form").trigger("validate.ajax.submit");
            var validateResult = $.bt_validate.result[$.bt_validate.form_id];
            
            return roadFeeConfirm && validateResult;
            
          }
        })
                      
    },
              
  }
})

